package com.jxd.ordersys.service.impl;

import com.jxd.ordersys.dao.OrderEmpMapper;
import com.jxd.ordersys.dao.OrderMealMapper;
import com.jxd.ordersys.dao.OrderReviewMapper;
import com.jxd.ordersys.dao.OrdersMapper;
import com.jxd.ordersys.model.*;
import com.jxd.ordersys.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OrdersServiceImpl
 * @Description TODO
 * @Author 王云超
 * @Date 2024/9/19 16:46
 * @Version 1.0
 */
@Service
public class OrdersServiceImpl implements IOrdersService {
    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    OrderMealMapper orderMealMapper;

    @Autowired
    OrderEmpMapper orderEmpMapper;

    @Autowired
    OrderReviewMapper orderReviewMapper;



    @Override
    public boolean delOrder(List<Integer> orderIds) {
        return ordersMapper.delOrder(orderIds) > 0;
    }

    @Override
    public boolean OrderExists(Integer empId) {
        return ordersMapper.OrderExists(empId);
    }

    @Override
    public Orders selectOrderByEmpId(Integer empId) {
        return ordersMapper.selectOrderByEmpId(empId);
    }

    @Override
    public boolean addOrder(Orders order) {
        return ordersMapper.addOrder(order) > 0;
    }

    @Override
    public boolean editOrder(Orders order) {
        return ordersMapper.editOrder(order)>0;
    }

    @Override
    public boolean createOrEditOrder(OrderRequest orderRequest) {
        try {
            // 解析数据,分别根据是提交还是保存操作数据库
            //获取订单id,可能为空,后面会做判断
            Integer orderId = orderRequest.getOrderId();
            //订餐发起人id
            Integer empId = orderRequest.getEmpId();
            //订餐人员列表
            List<OrderEmp> empList = orderRequest.getEmpList();
            //订单餐品关系列表
            List<OrderMeal> mealList = orderRequest.getMealList();
            //订餐总金额
            Double totalPrice = orderRequest.getTotalPrice();
            //订餐备注
            String orderNote = orderRequest.getOrderNote();
            //当前是点击的保存还是提交订餐,保存则状态为未提交,提交则修改状态为待部门经理审核
            Boolean isSubmit = orderRequest.getSubmitFlag();

            //创建Order对象
            Orders order = new Orders();
            order.setEmpId(empId);
            order.setOrderdate(getDate());
            order.setTotalprice(totalPrice);
            order.setOrderNote(orderNote);

            //判断是保存还是提交
            if (isSubmit){
                order.setStatus(1); //提交则订单状态设置为已提交,待部门经理审核
            }else {
                order.setStatus(0); //如果是保存,则是未提交状态,保存后仍然可编辑
            }

            // 判断是否是新订单，如果是新订单则插入，否则更新
            if (order.getOrderId() == null) {
                ordersMapper.addOrder(order);  // 插入订单并获取自增主键
                // 获取插入后的订单ID
                orderId = order.getOrderId();
            } else {
                ordersMapper.editOrder(order);  // 更新订单
            }

            // 更新或插入订单员工关系
            orderEmpMapper.delByOrderId(orderId);  // 删除旧的订餐员工关系
            for (OrderEmp orderEmp : empList) {
                orderEmp.setOrderId(orderId);  // 设置订单ID
                orderEmpMapper.addOrderEmp(orderEmp);  // 插入新关系
            }

            // 更新或插入订单餐品关系
            orderMealMapper.delByOrderId(orderId);  // 删除旧的订餐餐品关系
            for (OrderMeal orderMeal : mealList) {
                orderMeal.setOrderId(orderId);  // 设置订单ID
                orderMealMapper.addOrderMeal(orderMeal);  // 插入新关系
            }
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public boolean reviewOrder(Integer orderId, String reviewerName, String reviewNote, Integer reviewResult) {
        try {
            //根据orderId获取Order
            Orders order = ordersMapper.getOrderById(orderId);

            // 根据审核结果更新状态
            if (reviewResult == 1) {
                if (order.getStatus() == 1) {
                    order.setStatus(2); // 部门经理审核通过,状态改为待管理员审核
                } else if (order.getStatus() == 2) {
                    order.setStatus(3); // 管理员审核通过,状态改为审核完成
                }
            } else {
                order.setStatus(0); // 审核退回,可重新编辑
            }
            //更新订单审核状态
            ordersMapper.editOrder(order);

            //插入审核记录
            OrderReview orderReview = new OrderReview();
            orderReview.setOrderId(orderId);
            orderReview.setReviewerName(reviewerName);
            orderReview.setReviewNote(reviewNote);
            orderReview.setReviewResult(reviewResult);

            orderReviewMapper.addOrderReview(orderReview);
            return true;
        }catch (Exception e){
            return false;
        }

    }

    /**
     * 获取当前日期
     * @return
     */
    private String getDate(){
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }
}